<template>
  <div>
    <a-modal
      :title="isEdit ? '编辑' : '新增'"
      :visible="visible"
      :confirm-loading="confirmLoading"
      :width="600"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-form-model
        ref="ruleForm"
        :model="formData"
        :rules="rules"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 20 }"
      >
        <a-form-model-item label="选择人员" prop="personKey">
          <a-select
            v-model="formData.personKey"
            show-search
            placeholder="请选择"
            option-filter-prop="children"
            style="width: 100%"
            :filter-option="filterOption"
          >
            <a-select-option v-for="(item, index) in peopleList" :key="index" :value="item.pkey"
              >{{ item.person_name }}-{{ item.dep_name }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="选择设备" prop="controlKey">
          <a-select
            v-model="formData.controlKey"
            show-search
            placeholder="请选择"
            option-filter-prop="children"
            style="width: 100%"
            :filter-option="filterOption"
          >
            <a-select-option v-for="(item, index) in controlList" :key="index" :value="item.pkey"
              >{{ item.control_name }}-{{ item.control_ip }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="时间" prop="time">
          <a-date-picker
            v-model="formData.time"
            style="width: 100%"
            show-time
            placeholder="请选择"
            valueFormat="YYYY-MM-DD HH:mm:ss"
          ></a-date-picker>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>
    
    <script>
import { getPeopleSelectListApi } from '@/api/people'
import { getControlSelectListApi,postInoutrecordApi } from '@/api/attendance'
export default {
  data() {
    return {
      isEdit: false,
      visible: false,
      confirmLoading: false,
      formData: {
        personKey: '', // 人员
        controlKey: '', // 设备
        time: '', // 时间
      },
      rules: {
        personKey: [{ required: true, message: '请选择人员', trigger: 'change' }],
        controlKey: [{ required: true, message: '请选择设备', trigger: 'change' }],
        time: [{ required: true, message: '请选择时间', trigger: 'change' }],
      },
      peopleList: [],
      controlList: [],
    }
  },
  created() {
    this.getPeopleSelectList()
    this.getControlSelectList()
  },
  methods: {
    add() {
      this.isEdit = false
      this.visible = true
      this.formData.personKey = ''
      this.formData.controlKey = ''
      this.formData.time = ''
    },
    handleOk() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.confirmLoading = true
          let formData = { ...this.formData }
          const { statusCode, data, errors } = await postInoutrecordApi(formData)
          this.confirmLoading = false
          if (statusCode == 200) {
            this.visible = false
            this.$message.success('添加成功!')
            this.$parent.getTableData()
          } else {
            this.$message.error(errors)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleCancel() {
      this.visible = false
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    // 获取人员下拉列表
    async getPeopleSelectList() {
      const { data } = await getPeopleSelectListApi({
        count: 9999999,
      })
      this.peopleList = data
    },
    // 获取设备下拉列表
    async getControlSelectList() {
      const { data } = await getControlSelectListApi()
      this.controlList = data
    },
  },
}
</script>
    
    <style lang="scss" scoped>
</style>